<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExport\GuideExpoter;
use App\Admin\Extensions\Tools\ImportButton;
use App\Models\Guide;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class GuideController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('常德市导游信息');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Guide::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->tools(function ($tools) {
                $tools->append(new ImportButton('guides'));
            });
            $grid->disableActions();
            $grid->exporter(new GuideExpoter());
            $grid->id('ID')->sortable();
            $grid->name('姓名');
            $grid->sex('性别')->display(function ($val){
                return config('options.sex')[$val];
            });
            $grid->id_code('身份证号');
            $grid->qualifications_code('资格证号');
            $grid->code('导游证号');
            $grid->phone('电话');
            $grid->level('等级')->display(function ($val){
                return config('options.guide_level')[$val];
            });
//            $grid->star('星级')->display(function ($val){
//                return $val=!null?config('options.guide_star')[$val]:'';
//            });
            $grid->languages('语种')->display(function ($val){
                return config('options.guides_languages')[$val];
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Guide::class, function (Form $form) {
            $form->text('name','姓名');
            $form->radio('sex','性别')->options(config('options.sex'));
            $form->select('languages','语种')->options(config('options.guides_languages'));
            $form->select('level','等级')->options(config('options.guide_level'));
            //$form->select('star','星级')->options(config('options.guide_star'));
            $form->text('id_code','身份证号');
            $form->text('qualifications_code','资格证号');
            $form->text('code','导游证号');
            $form->text('phone','电话');

        });
    }
}
